package langchain4j.controller;

import langchain4j.bean.Appointment;
import langchain4j.service.AppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/appointments")
public class AppointmentsController {
    @Autowired
    private AppointmentService service;

    @GetMapping("/{userId}") // 更清晰的URL设计
    public ResponseEntity<Map<String, Object>> getUserAppointments(
            @PathVariable("userId") Long userId) {

        // 假设有service方法按用户ID查询列表
        List<Appointment> appointments = service.listByUserId(userId);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("data", appointments);
        return ResponseEntity.ok(response);
    }

    @PostMapping()
    @Transactional
    public ResponseEntity<Map<String, Object>> addAppointment(@Valid @RequestBody Appointment appointment) {
        Map<String, Object> response = new HashMap<>();

        try {
            // 1. 增强字段验证
            if (appointment.getUserId() == null) {
                response.put("code", 400);
                response.put("message", "用户ID不能为空");
                return ResponseEntity.badRequest().body(response);
            }

            // 2. 业务处理
            boolean saveSuccess = service.save(appointment);

            if (!saveSuccess) {
                response.put("code", 500);
                response.put("message", "保存预约失败，请稍后重试");
                return ResponseEntity.ok(response);
            }

            // 3. 返回完整响应
            response.put("code", 200);
            response.put("message", "预约成功");
            response.put("data", appointment);
            return ResponseEntity.ok(response);

        } catch (Exception e) {
            response.put("code", 500);
            response.put("message", "服务器错误: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }

    @PutMapping("/{id}/cancel")
    public ResponseEntity<Map<String, Object>> cancelAddAppointment(
            @PathVariable("id") Integer id) {

        Map<String, Object> response = new HashMap<>();

        try {
            // 1. 检查预约是否存在
            Appointment existing = service.getById(id);
            if (existing == null) {
                response.put("code", 404);
                response.put("message", "预约记录不存在");
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
            }

            // 2. 检查是否已取消
            if ("cancelled".equals(existing.getStatus())) {
                response.put("code", 200);
                response.put("message", "预约已是取消状态");
                return ResponseEntity.ok(response);
            }

            // 3. 执行取消操作
            boolean success = service.cancelAddAppointment(id);

            if (success) {
                response.put("code", 200);
                response.put("message", "预约取消成功");
                return ResponseEntity.ok(response);
            } else {
                response.put("code", 500);
                response.put("message", "取消预约失败");
                return ResponseEntity.internalServerError().body(response);
            }

        } catch (Exception e) {
            response.put("code", 500);
            response.put("message", "服务器错误: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
}